package com.hzqy.web.ac.industry.medical;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hzqy.service.industry.medical.AlertDataInfoService;
import com.hzqy.service.industry.medical.AlertInfoService;
import com.hzqy.service.industry.medical.InformationSynService;
import com.hzqy.web.vo.AlertDataInfoVo;
import com.hzqy.web.vo.AlertInfoVo;
import com.hzqy.web.vo.InformationSynVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hzqy.commons.utils.HttpUtil;
import com.hzqy.service.industry.medical.IndustryMedicalServiceBean;
import com.hzqy.web.vo.industry.medical.ItvConsumerMedicalVo;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

@Controller
@RequestMapping("/industry/interface/mdical")
public class IndustryMedicalAction{
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private IndustryMedicalServiceBean industryMedicalServiceBean;
	
	@Resource
    private AlertInfoService alertInfoService;
	@Resource
    private AlertDataInfoService alertDataInfoService;
	@Resource
    private InformationSynService informationSynService;
	
	@RequestMapping(value="/consumer", method=RequestMethod.GET)
	@ResponseBody
    public String getConsumerList(HttpServletRequest request,HttpServletResponse response) {
		String xx="[{ \"notifyType\": \"bloodpressure\",  \"data\": { \"lowPressure\": 80, \"heartRate\": 76,  \"highPressure\": 120  } }{ \"notifyType\": \"weight\",  \"data\": { \"weight\": 80  } }{ \"notifyType\": \"bracelet\", \"data\": { \"HeartRateData\": 80 } } { \"notifyType\": \"bracelet\",  \"data\": { \"SportStepData\": 100 } }]";
    	JSONObject obj=new JSONObject();
    	obj.put("code", 0);
    	obj.put("devices", JSON.parseArray(xx));
		
    	 BASE64Encoder encoder = new BASE64Encoder();
 		LOG.debug("收到GET请求"+request.getParameterMap());
 		BASE64Decoder decode=new BASE64Decoder ();
 		String xxx=encoder.encode(obj.toJSONString().getBytes());
		String str="eyJjb2RlIjowLCJkZXZpY2VzIjpbeyJub3RpZnlUeXBlIjoiYmxvb2RwcmVzc3VyZSIsImRhdGEi\r\nOnsibG93UHJlc3N1cmUiOjgwLCJoZWFydFJhdGUiOjc2LCJoaWdoUHJlc3N1cmUiOjEyMH19LHsi\r\nbm90aWZ5VHlwZSI6IndlaWdodCIsImRhdGEiOnsid2VpZ2h0Ijo4MH19LHsibm90aWZ5VHlwZSI6\r\nImJyYWNlbGV0IiwiZGF0YSI6eyJIZWFydFJhdGVEYXRhIjo4MH19LHsibm90aWZ5VHlwZSI6ImJy\r\nYWNlbGV0IiwiZGF0YSI6eyJTcG9ydFN0ZXBEYXRhIjoxMDB9fV19";
		return str;
    }
//	/**
//	 * 添加用户
//	 * @param request
//	 * @param response
//	 * @return
//	 */
    @RequestMapping(value="/consumer", method=RequestMethod.POST)
    @ResponseBody
    public Object postConsumer(HttpServletRequest request) {
    	Map<String,Object> resultMap = new HashMap<String, Object>();
    	 BufferedReader br = null;
         StringBuilder sb = new StringBuilder("");
         JSONObject obj=null;
         try {
             br = request.getReader();
             String str;
             while ((str = br.readLine()) != null) {
                 sb.append(str);
             }
             br.close();
             String str1=sb.toString();
             LOG.debug("str1"+str1);
             if(str1.indexOf("\"")==0&&str1.lastIndexOf("\"")==str1.length()-1||str1.indexOf("'")==0&&str1.lastIndexOf("'")==str1.length()-1){//置换
            	 str1=str1.substring(1,str1.length()-1);
    	    }
             BASE64Decoder decode=new BASE64Decoder ();
             str1 = new String(decode.decodeBuffer(str1),"UTF-8");
             obj=JSON.parseObject(str1);
         	if(str1!=null&&str1.length()>0){
         		resultMap=industryMedicalServiceBean.postConsumer(obj);
         	}else{
         		resultMap.put("code", -1);
         		resultMap.put("desc", "绑定用户同步失败");
         		resultMap.put("data", "");
         	}
         }catch (IOException e){
             LOG.error("读取请求参数错误",e);
             resultMap.put("code", -1);
      		resultMap.put("desc", "参数解析错误");
      		resultMap.put("data", "");
         }finally{
             if (null != br){
                 try{
                     br.close();
                 }catch (IOException e){
                     e.printStackTrace();
                 }
             }
         }
        return JSON.toJSON(resultMap);
    }
 
    @RequestMapping(value="/consumer", method=RequestMethod.PUT)
    public String putConsumer(HttpServletRequest request) {
    	LOG.debug("收到PUT请求id+"+request.getParameterMap());
        return "fail";
    }
 
    @RequestMapping(value="/consumer", method=RequestMethod.DELETE)
    public String deleteConsumer(HttpServletRequest request) {
    	LOG.debug("收到DELETE请求id+"+request.getParameterMap());
        return "fail";
    }
    
    @ResponseBody
    @RequestMapping("/addAlertInfo")
    public Object addOrderInfo(HttpServletRequest request){
        Map<String,String> resultMap=new HashMap();
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder("");
        JSONObject obj=null;
        try {
            br = request.getReader();
            String str;
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            br.close();
            String str1=sb.toString();
            LOG.debug("str1"+str1);
            if(str1.indexOf("\"")==0&&str1.lastIndexOf("\"")==str1.length()-1||str1.indexOf("'")==0&&str1.lastIndexOf("'")==str1.length()-1){//置换
                str1=str1.substring(1,str1.length()-1);
            }
            obj=JSON.parseObject(str1);
            if(str1!=null&&str1.length()>0){
                int i = alertInfoService.addAlertInfo(obj);
                LOG.debug("请求结果w为+"+i);
                if (i>0){
                    resultMap.put("code","200");
                    resultMap.put("message","接收推送数据成功！");
                }else {
                    resultMap.put("code","400");
                    resultMap.put("message","接收推送数据失败！");
                    LOG.debug("接收推送数据失败！");
                }
            }else{
                resultMap.put("code", "400");
                resultMap.put("message", "接收推送数据失败！!");
                LOG.debug("接收推送数据失败！!");
            }
        }catch (IOException e){
            LOG.error("读取请求参数错误",e);
            resultMap.put("code", "401");
            resultMap.put("message", "参数解析错误");
        }finally{
            if (null != br){
                try{
                    br.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return JSON.toJSON(resultMap);
    }
    @ResponseBody
    @RequestMapping("/selectAlertInfo")
    public Map<String,Object> selectOrderInfo(String f_al_equ_id,String f_al_equ_type,String f_al_type,String f_al_data,String f_al_pushTime,String f_al_createTime){
        Map<String,Object> resultMap=new HashMap();
        List<AlertInfoVo> alertInfoVoList;
        AlertInfoVo alertInfoVo=new AlertInfoVo();
        int type=0;
        if (f_al_type!=null&&!"".equals(f_al_type)){
            type = Integer.valueOf(f_al_type);
        }
        alertInfoVo.setF_al_equ_id(f_al_equ_id);
        alertInfoVo.setF_al_equ_type(f_al_equ_type);
        alertInfoVo.setF_al_type(type);
        alertInfoVo.setF_al_data(f_al_data);
        alertInfoVo.setF_al_pushTime(f_al_pushTime);
        alertInfoVo.setF_al_createTime(f_al_createTime);
        alertInfoVoList =alertInfoService.selectAlertInfo(alertInfoVo);
        if (alertInfoVoList==null){
            resultMap.put("code",400);
            resultMap.put("msg","失败");
            return resultMap;
        }
        resultMap.put("data",alertInfoVoList);
        resultMap.put("code",200);
        resultMap.put("msg","成功");
        return resultMap;
    }
    @ResponseBody
    @RequestMapping("/selectAlertInfoByPage")
    public Map<String,Object> selectOrderInfoByPage(String f_al_equ_id,String f_al_equ_type,String f_al_type,String f_al_data,String f_al_pushTime,String f_al_createTime,  Integer pageSize, Integer pageNum){
        Map<String,Object> resultMap=new HashMap();
        if (pageNum==null||pageSize==null){
            resultMap.put("code",402);
            resultMap.put("msg","页数或页面大小请输入完整");
            return resultMap;
        }
        if (pageNum<=0||pageSize<=0){
            resultMap.put("code",401);
            resultMap.put("msg","没有此页码");
            return resultMap;
        }
        List<AlertInfoVo> alertInfoVoList;
        AlertInfoVo alertInfoVo=new AlertInfoVo();
        int type=0;
        if (f_al_type!=null&&!"".equals(f_al_type)){
            type = Integer.valueOf(f_al_type);
        }
        alertInfoVo.setF_al_equ_id(f_al_equ_id);
        alertInfoVo.setF_al_equ_type(f_al_equ_type);
        alertInfoVo.setF_al_type(type);
        alertInfoVo.setF_al_data(f_al_data);
        alertInfoVo.setF_al_pushTime(f_al_pushTime);
        alertInfoVo.setF_al_createTime(f_al_createTime);
        alertInfoVoList =alertInfoService.selectAlertInfoByPage(alertInfoVo,pageSize,pageNum);
        if (alertInfoVoList==null){
            resultMap.put("code",400);
            resultMap.put("msg","失败");
            return resultMap;
        }
        resultMap.put("data",alertInfoVoList);
        resultMap.put("code",200);
        resultMap.put("msg","成功");
        return resultMap;
    }
    
    @ResponseBody
    @RequestMapping("/addAlertDataInfo")
    public Object addOrderDataInfo(HttpServletRequest request){
        Map<String,String> resultMap=new HashMap();
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder("");
        JSONObject obj=null;
        try {
            br = request.getReader();
            String str;
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            br.close();
            String str1=sb.toString();
            LOG.debug("str1"+str1);
            if(str1.indexOf("\"")==0&&str1.lastIndexOf("\"")==str1.length()-1||str1.indexOf("'")==0&&str1.lastIndexOf("'")==str1.length()-1){//置换
                str1=str1.substring(1,str1.length()-1);
            }
//            BASE64Decoder decode=new BASE64Decoder ();
//            str1 = new String(decode.decodeBuffer(str1),"UTF-8");
            obj=JSON.parseObject(str1);
            if(str1!=null&&str1.length()>0){
                int i = alertDataInfoService.addAlertDataInfo(obj);
                LOG.debug("请求结果w为+"+i);
                if (i>0){
                    resultMap.put("code","200");
                    resultMap.put("message","接收推送数据成功！");
                }else {
                    resultMap.put("code","400");
                    resultMap.put("message","接收推送数据失败！");
                    LOG.debug("接收推送数据失败！");
                }
            }else{
                resultMap.put("code", "400");
                resultMap.put("message", "接收推送数据失败！!");
                LOG.debug("接收推送数据失败！!");
            }
        }catch (IOException e){
            LOG.error("读取请求参数错误",e);
            resultMap.put("code", "401");
            resultMap.put("message", "参数解析错误");
        }finally{
            if (null != br){
                try{
                    br.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return JSON.toJSON(resultMap);
    }
    @ResponseBody
    @RequestMapping("/selectAlertDataInfo")
    public Map<String,Object> selectOrderDataInfo(AlertDataInfoVo alertDataInfoVo){
        Map<String,Object> resultMap=new HashMap();
        List<AlertDataInfoVo> alertDataInfoVoList;
        alertDataInfoVoList =alertDataInfoService.selectAlertDataInfo(alertDataInfoVo);
        if (alertDataInfoVoList==null){
            resultMap.put("code",400);
            resultMap.put("msg","失败");
            return resultMap;
        }
        resultMap.put("data",alertDataInfoVoList);
        resultMap.put("code",200);
        resultMap.put("msg","成功");
        return resultMap;
    }
    @ResponseBody
    @RequestMapping("/selectAlertDataInfoByPage")
    public Map<String,Object> selectOrderDataInfoByPage(AlertDataInfoVo alertDataInfoVo, Integer pageSize, Integer pageNum){
        Map<String,Object> resultMap=new HashMap();
        List<AlertDataInfoVo> alertDataInfoVoList;
        if (pageNum==null||pageSize==null){
            resultMap.put("code",402);
            resultMap.put("msg","页数或页面大小请输入完整");
            return resultMap;
        }
        if (pageNum<=0||pageSize<=0){
            resultMap.put("code",401);
            resultMap.put("msg","没有此页码");
            return resultMap;
        }
        alertDataInfoVoList =alertDataInfoService.selectAlertDataInfoByPage(alertDataInfoVo,pageSize,pageNum);
        if (alertDataInfoVoList==null){
            resultMap.put("code",400);
            resultMap.put("msg","失败");
            return resultMap;
        }
        resultMap.put("data",alertDataInfoVoList);
        resultMap.put("code",200);
        resultMap.put("msg","成功");
        return resultMap;
    }
    
    @ResponseBody
    @RequestMapping("/addInformationSyn")
    public Object addInformationSyn(HttpServletRequest request){
        Map<String,String> resultMap=new HashMap();
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder("");
        JSONObject obj=null;
        try {
            br = request.getReader();
            String str;
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            br.close();
            String str1=sb.toString();
            LOG.debug("str1"+str1);
            if(str1.indexOf("\"")==0&&str1.lastIndexOf("\"")==str1.length()-1||str1.indexOf("'")==0&&str1.lastIndexOf("'")==str1.length()-1){//置换
                str1=str1.substring(1,str1.length()-1);
            }
            obj=JSON.parseObject(str1);
            if(str1!=null&&str1.length()>0){
                int i = informationSynService.addInformationSyn(obj);
                LOG.debug("请求结果w为+"+i);
                if (i>0){
                    resultMap.put("code","200");
                    resultMap.put("message","接收推送数据成功！");
                }else {
                    resultMap.put("code","400");
                    resultMap.put("message","接收推送数据失败！");
                    LOG.debug("接收推送数据失败！");
                }
            }else{
                resultMap.put("code", "400");
                resultMap.put("message", "接收推送数据失败！!");
                LOG.debug("接收推送数据失败！!");
            }
        }catch (IOException e){
            LOG.error("读取请求参数错误",e);
            resultMap.put("code", "401");
            resultMap.put("message", "参数解析错误");
        }finally{
            if (null != br){
                try{
                    br.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return JSON.toJSON(resultMap);
    }
    @ResponseBody
    @RequestMapping("/selectInformationSyn")
    public Map<String,Object> selectInformationSyn(InformationSynVo informationSynVo){
        Map<String,Object> resultMap=new HashMap();
        List<InformationSynVo> informationSynVos;
        informationSynVos =informationSynService.selectInformationSyn(informationSynVo);
        if (informationSynVos==null){
            resultMap.put("code",400);
            resultMap.put("msg","失败");
            return resultMap;
        }
        resultMap.put("data",informationSynVos);
        resultMap.put("code",200);
        resultMap.put("msg","成功");
        return resultMap;
    }
    @ResponseBody
    @RequestMapping("/selectInformationSynByPage")
    public Map<String,Object> selectInformationSynByPage(InformationSynVo informationSynVo, Integer pageSize, Integer pageNum){
        Map<String,Object> resultMap=new HashMap();
        List<InformationSynVo> informationSynVos;
        if (pageNum==null||pageSize==null){
            resultMap.put("code",402);
            resultMap.put("msg","页数或页面大小请输入完整");
            return resultMap;
        }
        if (pageNum<=0||pageSize<=0){
            resultMap.put("code",401);
            resultMap.put("msg","没有此页码");
            return resultMap;
        }
        informationSynVos =informationSynService.selectInformationSynByPage(informationSynVo,pageSize,pageNum);
        if (informationSynVos==null){
            resultMap.put("code",400);
            resultMap.put("msg","失败");
            return resultMap;
        }
        resultMap.put("data",informationSynVos);
        resultMap.put("code",200);
        resultMap.put("msg","成功");
        return resultMap;
    }
}


